{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2021-03-29 08:24:45] Try to use the default NATS-Bench (topology) path from fast_mode=True and path=None.\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "import numpy as np\n",
    "from nats_bench import create\n",
    "from pprint import pprint\n",
    "# Create the API for tologoy search space\n",
    "api_tss = create(None, 'tss', fast_mode=True, verbose=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def random_search(api, trials=20, dataset='ImageNet16-120'):\n",
    "    api.reset_time()\n",
    "    highest_accuracy, best_arch = -1, -1\n",
    "    for i in range(trials):\n",
    "        arch_index = random.randint(0, len(api)-1)\n",
    "        accuracy, _, _, total_cost = api.simulate_train_eval(\n",
    "            arch_index, dataset, hp=\"12\"\n",
    "        )\n",
    "        if accuracy > highest_accuracy:\n",
    "            highest_accuracy = accuracy\n",
    "            best_arch = arch_index\n",
    "    return arch_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Random Search on ImageNet16-120 : 33.9463 $\\pm$ 9.4213\n"
     ]
    }
   ],
   "source": [
    "# Just a small example, not the full experiment in the paper\n",
    "dataset = 'ImageNet16-120'\n",
    "rs_times, accuracies = 100, []\n",
    "for i in range(rs_times):\n",
    "    arch_index = random_search(api_tss, dataset=dataset)\n",
    "    info = api_tss.get_more_info(arch_index, dataset, hp='200', is_random=False)\n",
    "    accuracies.append(info['test-accuracy'])\n",
    "print('Random Search on {:} : {:.4f} $\\pm$ {:.4f}'.format(dataset, np.mean(accuracies), np.std(accuracies)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
